package com.example.administrator.lazero_xposed;

//package ai.lazero.lazero;

//import android.content.pm.ApplicationInfo;

import java.lang.reflect.Field;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

//import android.content.pm.PackageParser.Package;
//import com.android.server.pm.Settings;
//import de.robv.android.xposed.IXposedHookZygoteInit;
//import de.robv.android.xposed.mods.appsettings.XposedMod;
//import com.android.server.pm.permission.PermissionsState.PermissionState
public class HookTest_v0 implements IXposedHookLoadPackage {
    //    public boolean isExecuted = false;
//    public int counter = 3;
    // it is dead.
//    public static final int sysuid = 10173;
//    // root uid is 0.
//    public static final int flag_system = 1 << 0;
//    public static final int priv_flag = 1 << 3;
    //            private boolean grantSignaturePermission(String perm, PackageParser.Package pkg,
//                    BasePermission bp, PermissionsState origPermissions)
    //    final Class <?> test = XposedHelpers.findClass("com.android.server.pm.PackageManagerService",XC_LoadPackage.LoadPackageParam.class.getClassLoader());
//    final Class <?> package_clazz = XposedHelpers.findClass("android.content.pm.PackageParser.Package",XC_LoadPackage.LoadPackageParam.class.getClassLoader());
//    final Class <?> permission_clazz = XposedHelpers.findClass("com.android.server.pm.permission.PermissionsState.PermissionState",XC_LoadPackage.LoadPackageParam.class.getClassLoader());
//    final Class <?> basepermission_clazz = XposedHelpers.findClass("com.android.server.pm.permission.BasePermission",XC_LoadPackage.LoadPackageParam.class.getClassLoader());
//    final Class<?> class1 = XposedHelpers.findClass
//    final Class<?> clsPMS
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
//        Class clazz = loadPackageParam.classLoader.loadClass(
//        "com.android.server.pm.PackageManagerService");
        Class<?> clazz = loadPackageParam.classLoader.loadClass("com.android.server.pm.PackageManagerService");
        // are you sure to find this thing? not zygote init?
//        Class package_clazz = loadPackageParam.classLoader.loadClass("android.content.pm.PackageParser.Package");
//        Class permission_clazz = loadPackageParam.classLoader.loadClass("com.android.server.pm.permission.PermissionsState.PermissionState");
//        Class basepermission_clazz = loadPackageParam.classLoader.loadClass("com.android.server.pm.permission.BasePermission");
// hooking all methods will result in disaster.
//            XposedHelpers.find
        XposedHelpers.findAndHookMethod(clazz, "grantSignaturePermission", Object.class, Object.class, Object.class, Object.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                XposedBridge.log("Permission Signature method has Hooked! " + (String) param.args[0]);
                Object pkg = param.args[1];
//                pkg.getClass().getField()
                Field field = pkg.getClass().getField("packageName");
                String packageName = (String) field.get(pkg);
                XposedBridge.log("Permission Signature PackageName: " + packageName);
                if (packageName.equals("ai.lazero.lazero")) {
                    param.setResult(true);
                }
                super.beforeHookedMethod(param);
//                pkg.getClass().
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                // hook method and then change by the content.
                // restore permissions and do not modify the result.
                param.setResult(true);
                super.afterHookedMethod(param);
            }

        });

    }

}
